﻿using BLL;
using MODEL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Loginter.车辆管理
{
    public partial class TruckRepeater : System.Web.UI.Page
    { 
        //调用BLL层车辆信息
        SelectTruckBLL truck = new SelectTruckBLL();
        //调用BLL层的车队有关内容
        TruckTeamBLL truckteam = new TruckTeamBLL();
        //调用BLL层的车辆信息
        TruckBLL truckb = new TruckBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindtruckList();
                BindcheliangList();
                //BindTruckInfo();
                BindPagerIndex();
            }
        }
        /// <summary>
        /// 绑定所属车队列表
        /// </summary>
        private void BindtruckList()
        {
            DataTable table = truckteam.GetTruckList();
            drop_Truck.DataSource = table;
            drop_Truck.DataTextField = "TeamName";
            drop_Truck.DataValueField = "TeamID";
            drop_Truck.DataBind();
            drop_Truck.Items.Insert(0, new ListItem("不限", "0"));
        }

        /// <summary>
        /// 绑定车辆类型
        /// </summary>
        private void BindcheliangList()
        {
            DataTable table = truck.GetTruckList();
            drop_Type.DataSource = table;
            drop_Type.DataTextField = "Type";
            drop_Type.DataValueField = "Type";
            drop_Type.DataBind();
            drop_Type.Items.Insert(0, new ListItem("--全部--", "0"));
        }

        /// <summary>
        /// 分页事件
        /// </summary>
        /// <param name="src"></param>
        /// <param name="e"></param>
        protected void TruckPager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            TruckPager.CurrentPageIndex = e.NewPageIndex;
            BindPagerIndex();
        }

        /// <summary>
        /// 绑定存储空间事件
        /// </summary>
        private void BindPagerIndex()
        {
            string number = txt_Number.Text.Trim();
            //string type = drop_Type.SelectedValue;type,
            int totalCount;
            repe_Driver.DataSource = truckb.GetPagerTruckList(number,  TruckPager.CurrentPageIndex, TruckPager.PageSize, out totalCount);
            TruckPager.RecordCount = totalCount;
            repe_Driver.DataBind();
        }

        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Select_Click(object sender, EventArgs e)
        {
            TruckPager.CurrentPageIndex = 1;
            BindPagerIndex();
        }

        /// <summary>
        /// 添加按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddTruckList.aspx");
        }

        /// <summary>
        /// repeater删除事件
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void repe_Driver_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                int userid = Convert.ToInt32(e.CommandArgument);
                int result = truckb.DeleteTruckList(userid);
                if (result == 1)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('删除成功！');</script>");
                    BindPagerIndex();
                }
                else if (result == -1)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('该信息已删除！');</script>");
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('删除失败！');</script>");
                }
            }
        }

    }
}